const CourseModel = require('../models/CourseModel')

const CourseService = {
  add: async (body) => {
    const { courseid, coursename, courseteacher, major, classes, term, credit, type, ratio } = body
    return CourseModel.create({ courseid, coursename, courseteacher, major, classes, term, credit, type, ratio, ispublish: 0 })
  },

  getList: async ({ _id }, { term, coursename }, page, limit) => {
    if (_id) {
      return CourseModel.find({ _id })
    } else {
      if (term && coursename) {
        return CourseModel.find({ term, coursename })
          .sort({ _id: -1 })
          .skip((page - 1) * limit)
          .limit(limit)
      } else if (term) {
        return CourseModel.find({ term })
          .sort({ _id: -1 })
          .skip((page - 1) * limit)
          .limit(limit)
      } else if (coursename) {
        return CourseModel.find({ coursename })
          .sort({ _id: -1 })
          .skip((page - 1) * limit)
          .limit(limit)
      } else {
        return CourseModel.find()
          .sort({ _id: -1 })
          .skip((page - 1) * limit)
          .limit(limit)
      }
    }
  },

  getListPublished: async ({ ispublish }) => {
    return CourseModel.find({ ispublish })
  },

  getMyList: async ({ courseteacher }, { term, coursename }, page, limit) => {
    if (term && coursename) {
      return CourseModel.find({ courseteacher, term, coursename })
        .sort({ _id: -1 })
        .skip((page - 1) * limit)
        .limit(limit)
    } else if (term) {
      return CourseModel.find({ courseteacher, term })
        .sort({ _id: -1 })
        .skip((page - 1) * limit)
        .limit(limit)
    } else if (coursename) {
      return CourseModel.find({ courseteacher, coursename })
        .sort({ _id: -1 })
        .skip((page - 1) * limit)
        .limit(limit)
    } else {
      return CourseModel.find({ courseteacher })
        .sort({ _id: -1 })
        .skip((page - 1) * limit)
        .limit(limit)
    }
  },

  getMyListPublished: async ({ courseteacher, ispublish }) => {
    return CourseModel.find({ courseteacher, ispublish })
  },

  getStudentList: async ({ major }) => {
    return CourseModel.find({ major })
  },

  getStudentPublishedList: async ({ major }) => {
    return CourseModel.find({ major, ispublish: 1 })
  },

  putList: async ({ _id, courseid, coursename, courseteacher, major, classes, term, credit, type, ratio }) => {
    return CourseModel.updateOne({ _id }, { courseid, coursename, courseteacher, major, classes, term, credit, type, ratio })
  },

  delList: async ({ courseid }) => {
    return CourseModel.deleteOne({ courseid })
  },

  putPublish: async (courseid, { ispublish }) => {
    return CourseModel.updateOne({ courseid }, { ispublish })
  },

  getTotal: async ({ term, coursename }) => {
    if (term && coursename) {
      return CourseModel.countDocuments({ term, coursename })
    } else if (term) {
      return CourseModel.countDocuments({ term })
    } else if (coursename) {
      return CourseModel.countDocuments({ coursename })
    } else {
      return CourseModel.countDocuments()
    }
  },

  getMyTotal: async ({ courseteacher }, { term, coursename }) => {
    if (term && coursename) {
      return CourseModel.countDocuments({ courseteacher, term, coursename })
    } else if (term) {
      return CourseModel.countDocuments({ courseteacher, term })
    } else if (coursename) {
      return CourseModel.countDocuments({ courseteacher, coursename })
    } else {
      return CourseModel.countDocuments({ courseteacher })
    }
  }
}

module.exports = CourseService
